package net.maku.qdd.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;

import net.maku.qdd.common.utils.PageResult;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.maku.qdd.convert.TransactionConvert;
import net.maku.qdd.entity.TransactionEntity;
import net.maku.qdd.query.TransactionQuery;
import net.maku.qdd.vo.TransactionVO;
import net.maku.qdd.dao.TransactionDao;
import net.maku.qdd.service.TransactionService;
import net.maku.qdd.common.utils.ExcelUtils;
import net.maku.qdd.vo.TransactionExcelVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 交易记录
 *
 * @author MAKU babamu@126.com
 * <a href="https://maku.net">MAKU</a>
 */
@Service
public class TransactionServiceImpl extends ServiceImpl<TransactionDao, TransactionEntity> implements TransactionService {

    @Override
    public PageResult<TransactionVO> page(TransactionQuery query) {
        IPage<TransactionEntity> page = baseMapper.selectPage(getPage(query), getWrapper(query));

        return new PageResult<>(TransactionConvert.INSTANCE.convertList(page.getRecords()), page.getTotal());
    }





    @Override
    public TransactionVO get(Long id) {
        TransactionEntity entity = baseMapper.selectById(id);
        TransactionVO vo = TransactionConvert.INSTANCE.convert(entity);

        return vo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(TransactionVO vo) {
        TransactionEntity entity = TransactionConvert.INSTANCE.convert(vo);

        baseMapper.insert(entity);


    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(TransactionVO vo) {
        TransactionEntity entity = TransactionConvert.INSTANCE.convert(vo);

        updateById(entity);


    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> idList) {
        removeByIds(idList);

    }



    @Override
    public void export() {
        List<TransactionExcelVO> excelList = TransactionConvert.INSTANCE.convertExcelList(list());
        ExcelUtils.excelExport(TransactionExcelVO.class, "交易记录", null, excelList);
    }

    /**
     * 获取分页对象
     */
    private IPage<TransactionEntity> getPage(TransactionQuery query) {
        return new Page<>(query.getCurrent(), query.getSize());
    }

    /**
     * 获取查询包装器
     */
    private LambdaQueryWrapper<TransactionEntity> getWrapper(TransactionQuery query){
        LambdaQueryWrapper<TransactionEntity> wrapper = Wrappers.lambdaQuery();
        // 这里可以添加具体的查询条件
        return wrapper;
    }
}